package com.gdiss.www.handler;



//import com.auth0.jwt.exceptions.JWTVerificationException;
import com.gdiss.www.constant.ResultConstant;
import com.gdiss.www.constant.ValidationNameConstant;
import com.gdiss.www.vo.ResultVo;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingPathVariableException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import java.util.Objects;

/**
 * 全局捕获异常
 * @author yly
 * @date 2021/2/10 11:17
 */
@RestControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler  {


//    /**
//     * 如果异常产生于进入controller前，那么方法不可达
//     * jwt校验失败，返回密匙过期
//     */
//    @Deprecated
//    @ExceptionHandler(JWTVerificationException.class)
//    public ResponseEntity<Object> handleJwtVerificationException(JWTVerificationException ex){
//        return new ResponseEntity<>(
//                ResultConstant.TOKEN_EXPIRE,
//                HttpStatus.OK
//        );
//    }

//    /**
//     * 捕获全局500异常，统一格式返回
//     */
//    @ExceptionHandler(Exception.class)
//    public ResponseEntity<Object> handleException(Exception ex){
//        return new ResponseEntity<>(
//                ResultConstant.SYSTEM_ERROR,
//                HttpStatus.INTERNAL_SERVER_ERROR
//        );
//    }

    /**
     * 缺失请求参数
     */
    @Override
    protected ResponseEntity<Object> handleMissingServletRequestParameter(MissingServletRequestParameterException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
        System.out.println(ex.getParameterName());
        return new ResponseEntity<>(
                new ResultVo<>().emptyParamError(ex.getParameterName()),
                HttpStatus.BAD_REQUEST
        );
    }

    /**
     * 缺失请求参数
     */
    @Override
    protected ResponseEntity<Object> handleMissingPathVariable(MissingPathVariableException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
        System.out.println(ex.getVariableName());
        return new ResponseEntity<>(
                new ResultVo<>().emptyParamError(ex.getVariableName()),
                HttpStatus.BAD_REQUEST
        );
    }

    /**
     * 参数格式不正确
     */
    @Override
    protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {

        //空串处理
        if (ValidationNameConstant.NOT_EMPTY.equals(Objects.requireNonNull(ex.getBindingResult().getFieldError()).getCode())) {
            return new ResponseEntity<>(new ResultVo<>().emptyParamError(ex.getBindingResult().getFieldError().getField()), HttpStatus.BAD_REQUEST);
        }
        //其他格式校验处理
        return new ResponseEntity<>(new ResultVo<>().paramNotValid(ex.getBindingResult().getFieldError().getField()), HttpStatus.BAD_REQUEST);
    }

}
